# -*- coding: utf-8 -*-
'''
Created on 16-10-2012

@author: LONG HOANG GIANG
'''
from CrawlerLib2.commonlib import toAscii
from basemodel import SQLiteDatabase
import os
import re

class StoryModel(SQLiteDatabase):
    
    def __init__(self, database):
        folder = os.path.dirname(database)
        if folder != '':
            if not os.path.isdir(folder): os.makedirs(folder, 0777)
            SQLiteDatabase.__init__(self, database)
        
    def add_category(self, catName, root=0):
        if not isinstance(catName, unicode): catName = catName.decode('utf-8')
        kdCatName = toAscii(catName).lower().replace('-', ' ')
        kdCatName = re.sub(r"[^\w ]", "", kdCatName).strip()
        kdCatName = re.sub(r" +", " ", kdCatName)
        catid = self.db.execute("INSERT INTO vn_gq_udv_category(catName, root, kdCatName) VALUES(?, ?, ?)", (catName, root, kdCatName))
        self.db.commit()
        if catid == 0:
            self.db.execute("SELECT catId FROM vn_gq_udv_category WHERE catName=?", (catName))
            catid = self.db.fetchone()[0]
        return catid
    
    def get_category_by_id(self, cid):
        self.db.execute("SELECT catId, catName, root FROM vn_gq_udv_category WHERE catId=?", cid)
        return self.db.fetchone()
    
    def delete_category(self, catid):
        self.db.execute("DELETE FROM vn_gq_udv_category WHERE catId=?", [catid])
        
    def update_category(self, catid, catname, root=0):
        if not isinstance(catname, unicode): catname = unicode(catname)
        self.db.execute("UPDATE vn_gq_udv_category SET catName=?, root=? WHERE catId=?", (catname, root, catid))
    
    def add_story(self, title, detail, catid=0):
        if not isinstance(detail, unicode): detail = detail.decode('utf-8', 'ignore')
        if not isinstance(title, unicode): title = title.decode('utf-8', 'ignore')
        self.db.execute("INSERT INTO vn_gq_udv_story(catId, title, detail) VALUES(?, ?, ?)", (catid, title, detail))

    def get_category(self, root=0):
        self.db.execute("SELECT * FROM vn_gq_udv_category WHERE root={0}".format(root))
        rows = self.db.fetchall()
        return rows
    
    def get_all_categories(self):
        self.db.execute("SELECT * FROM vn_gq_udv_category")
        rows = self.db.fetchall()
        return rows
    
    def get_all_stories(self):
        self.db.execute("SELECT * FROM vn_gq_udv_story")
        return self.db.fetchall()
    
    def get_story_by_cat(self, catid):
        self.db.execute("SELECT * FROM vn_gq_udv_story WHERE catId={0}".format(catid))
        return self.db.fetchall()
        
    def get_story_by_id(self, storyId):
        self.db.execute("SELECT * FROM vn_gq_udv_story WHERE id={0}".format(storyId))
        return self.db.fetchone()

    def get_story_title(self):
        self.db.execute("SELECT id, title FROM vn_gq_udv_story")
        return self.db.fetchall()
    
    def delete_story_by_id(self, id):
        self.db.execute("DELETE FROM vn_gq_udv_story WHERE id={0}".format(id))
    
    def upate_story(self, title, detail, id):
        if not isinstance(title, unicode): title = title.decode('utf-8')
        if not isinstance(detail, unicode): detail = detail.decode('utf-8')
        self.db.execute("UPDATE vn_gq_udv_story SET title=?, detail=? WHERE id=?", (title, detail, id))

